package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import users.Manager;

import model.Item;

public class AddNewItem extends JPanel{

	private static final long serialVersionUID = 313945208014092049L;
	
	private Manager manager;
	private JTextField upcText, titleText, typeText, categoryText, companyText, yearText, sellPriceText;
	private JTextArea textArea;
	private JScrollPane confirmation;
	private JButton addItemButton;
	
	public AddNewItem(Manager manager){
		
		this.manager = manager;
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		inputFieldsPanel();
		
		buttonPanel();
		
		confirmPanel();
	}
	
	// add input fields
	private void inputFieldsPanel(){
		JPanel textFieldPanel = new JPanel(new GridLayout(7,2));
		textFieldPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		
		textFieldPanel.add(new JLabel("upc: "));
        upcText = new JTextField();
        upcText.setPreferredSize(new Dimension(300, 30));
        textFieldPanel.add(upcText);

        textFieldPanel.add(new JLabel("title: "));
        titleText = new JTextField();
        titleText.setPreferredSize(new Dimension(300, 30));
        textFieldPanel.add(titleText);
        
        textFieldPanel.add(new JLabel("type: "));
        typeText = new JTextField();
        typeText.setPreferredSize(new Dimension(300, 30));
        textFieldPanel.add(typeText);
        
        textFieldPanel.add(new JLabel("category: "));
        categoryText = new JTextField();
        categoryText.setPreferredSize(new Dimension(300, 30));
        textFieldPanel.add(categoryText);
        
        textFieldPanel.add(new JLabel("company: "));
        companyText = new JTextField();
        companyText.setPreferredSize(new Dimension(300, 30));
        textFieldPanel.add(companyText);
        
        textFieldPanel.add(new JLabel("year: "));
        yearText = new JTextField();
        yearText.setPreferredSize(new Dimension(300, 30));
        textFieldPanel.add(yearText);
        
        textFieldPanel.add(new JLabel("sellPrice: "));
        sellPriceText = new JTextField();
        sellPriceText.setPreferredSize(new Dimension(300, 30));
        textFieldPanel.add(sellPriceText);
        
        add(textFieldPanel);
        
	}
	
	// button for adding item
	private void buttonPanel() {
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		
		addItemButton = new JButton("Add Item");
		addItemButton.addActionListener(new addItem());
        buttonPanel.add(addItemButton, BorderLayout.CENTER);
		
		add(buttonPanel);
	}
	
	// add the text area to confirm item addition
	private void confirmPanel(){
		JPanel conPanel = new JPanel(new BorderLayout());
		TitledBorder title;
		title = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Registration Confirmation");
		conPanel.setBorder(title);
		
        textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        
        confirmation = new JScrollPane(textArea);
        confirmation.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        
        conPanel.add(confirmation, BorderLayout.CENTER);
		
        add(conPanel);
	}

	// listener for button
	private class addItem implements ActionListener{
		
		String upc, title, type, category, company, year, sellPrice;
		
		public void actionPerformed(ActionEvent e){
			// get the text field data
			upc = upcText.getText();
			title = titleText.getText();
			type = typeText.getText();
			category = categoryText.getText();
			company = companyText.getText();
			year = yearText.getText();
			sellPrice = sellPriceText.getText();
						
			// if any of the fields are not filled in
			if (upc.isEmpty() || sellPrice.isEmpty()){
				JOptionPane.showMessageDialog(addItemButton,
					    "Please check that the following have been entered:\n- upc\n- sell price",
					    "Registration Error",
					    JOptionPane.ERROR_MESSAGE);
			} else { 
				try {
						manager.addItem(upc, title, type, category, company, year, sellPrice);
					
						//add information in the confirmation window
						textArea.append("******************* Item Added ******************" + "\n" +
										"\n" + "upc: " + upc + 
										"\n" + "Title: " + title + 
										"\n" + "Type: " + type + 
										"\n" + "Category: " + category + 
										"\n" + "Company: " + company +
										"\n" + "year " + year +
										"\n" + "Sell Price: " + sellPrice +
										"\n\n" + 
										"*********************************************************" + "\n\n");
					
				// if the primary key is violated (same upc)
				} catch (SQLException e1) {
					JOptionPane.showMessageDialog(addItemButton,
						    "Item Addition Failed.\nPlease check that all fields are properly filled in.",
						    "Registration Error",
						    JOptionPane.ERROR_MESSAGE);
					
					JOptionPane.showMessageDialog(addItemButton, e1);
				}
			}
	    }
	}
}
